package com.chenhongjian.webapp.controller;

import com.chenhongjian.webapp.bean.TbUser;
import com.chenhongjian.webapp.config.BaseResponse;
import com.chenhongjian.webapp.config.LoginInterceptor;
import com.chenhongjian.webapp.config.WebAppConfig;
import com.chenhongjian.webapp.service.IUserService;

import com.chenhongjian.webapp.utils.ImageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by a on 2018/3/27.
 */
@RestController
@RequestMapping(value = "/api")
public class UserController {

    @Autowired
    IUserService userService;
    @RequestMapping(value = "/phoneLogin", method = {RequestMethod.GET })
    public BaseResponse<TbUser> phoneLogin(@RequestParam(value = "phone", required = true) String phone,
                                   @RequestParam(value = "code", required = true) String code,HttpSession session){
        TbUser user = userService.loginPhone(phone, code);

        if(user != null){
            session.setAttribute(WebAppConfig.LOGIN_USER,user);
        }

        return BaseResponse.getDefaultSuccess(user);
    }


    @RequestMapping(value = "/saveUserByNameAndPhone", method = {RequestMethod.GET })
    public BaseResponse<TbUser> saveUserByNameAndPhone(@RequestParam(value = "phone", required = true) String phone,
                                           @RequestParam(value = "username", required = true) String username,HttpSession session){

        TbUser user = userService.saveUserByNameAndPhone(phone, username);
        if(user != null){
            session.setAttribute(WebAppConfig.LOGIN_USER,user);
            if(user.getArea() != null && user.getArea().equals("0")){
                return BaseResponse.getDefaultSuccess("0",user);
            }
        }

        return BaseResponse.getDefaultSuccess(user);
    }

    private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

    @RequestMapping(value = "/upload_content",method = {RequestMethod.POST })
    public BaseResponse<Object>  uploadContent( @RequestParam("contentInput") String contentInput,HttpSession session) {

        BaseResponse<Object> result = null;
        TbUser user = null;
        if(session.getAttribute(WebAppConfig.LOGIN_USER) != null){
            user = (TbUser) session.getAttribute(WebAppConfig.LOGIN_USER);
        }

        if(user != null){
              user.setLocation(contentInput);
              userService.updateUserinfo(user);
              result = BaseResponse.getDefaultSuccess(null);
          }else{

            result = BaseResponse.getDefatultError("上传失败！");
             }
        return result;
    }


        @RequestMapping(value = "/upload",method = {RequestMethod.POST })
    public BaseResponse<Object>  uploadImg(@RequestParam("file") MultipartFile multipartFile,HttpSession session)  {


        String root_fileName = multipartFile.getOriginalFilename();
        String contentType = multipartFile.getContentType();
        logger.info("上传图片:name={},type={}", root_fileName, contentType);
        String filePath = "./src/main/resources/static/images/upload";
        logger.info("图片保存路径={}", filePath);
        String file_name = null;

        //filename 获取session 中用户的手机号
        TbUser user = null;
        if(session.getAttribute(WebAppConfig.LOGIN_USER) != null){
            user = (TbUser) session.getAttribute(WebAppConfig.LOGIN_USER);
        }

        try {

            String filename = user==null?"usernophone":user.getMobileNumber();

            file_name = ImageUtil.saveImg(multipartFile, filePath,filename);
            //保存数据库 dojob





         } catch (IOException e) {

        }


        BaseResponse defaultSuccess = BaseResponse.getDefaultSuccess("");
         defaultSuccess.setMessage(file_name);

         return defaultSuccess;
    }

    /**
     * 投票
     */

    @RequestMapping(value = "/vote",method = {RequestMethod.GET })
    public BaseResponse<TbUser>  vote(@RequestParam(value = "phone", required = true) String phone,
                     HttpSession session){

        BaseResponse<TbUser> result = null;

        TbUser user = null;
        if(session.getAttribute(WebAppConfig.LOGIN_USER) != null){
            user = (TbUser) session.getAttribute(WebAppConfig.LOGIN_USER);
        }

        if(user ==null){
           result =  BaseResponse.getDefatultError(null);
           result.setMessage("请登录在投票");
           return result;
        }


        userService.vote(user,phone);

        result =  BaseResponse.getDefaultSuccess("投票成功!");

        return result;

    }



    @RequestMapping(value = "/list",method = {RequestMethod.GET })
    public BaseResponse<List<TbUser>>  getListUser(HttpSession session){

        List<TbUser> list =  userService.getList();

        BaseResponse<List<TbUser>> r =   BaseResponse.getDefaultSuccess(null);

        r.setData(list);


        return r;


    }


}
